<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="推送分类" prop="channelType">
        <el-select v-model="queryParams.channelType" placeholder="请选择推送分类" clearable size="small">
          <el-option
            v-for="dict in channelTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送通道类型" prop="channelServiceCode">
        <el-input
          v-model="queryParams.channelServiceCode"
          placeholder="请输入推送通道类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operate:pushconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operate:pushconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operate:pushconfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['operate:pushconfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pushconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="推送分类" align="center" prop="channelType" :formatter="channelTypeFormat" />
      <el-table-column label="推送通道类型" align="center" prop="channelServiceCode" />
      <el-table-column label="推送通道类型名称" align="center" prop="channelServiceName" />
      <el-table-column label="通道名称" align="center" prop="channelName" />
      <el-table-column label="弹窗" align="center" prop="popupWindow" />
      <el-table-column label="推送" align="center" prop="push" />
      <el-table-column label="webhook" align="center" prop="webhook" />
      <el-table-column label="参数" align="center" prop="sendIdParam" />
      <el-table-column label="内容参数" align="center" prop="textMsg" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="是否被删除" align="center" prop="deleted" :formatter="deletedFormat" />
      <el-table-column label="是否在使用" align="center" prop="isInUse" :formatter="isInUseFormat" />
      <el-table-column label="jar文件" align="center" prop="jarId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operate:pushconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operate:pushconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改推送通道配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="推送分类" prop="channelType">
          <el-select v-model="form.channelType" placeholder="请选择推送分类">
            <el-option
              v-for="dict in channelTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推送通道类型" prop="channelServiceCode">
          <el-input v-model="form.channelServiceCode" placeholder="请输入推送通道类型" />
        </el-form-item>
        <el-form-item label="推送通道类型名称" prop="channelServiceName">
          <el-input v-model="form.channelServiceName" placeholder="请输入推送通道类型名称" />
        </el-form-item>
        <el-form-item label="通道名称" prop="channelName">
          <el-input v-model="form.channelName" placeholder="请输入通道名称" />
        </el-form-item>
        <el-form-item label="弹窗" prop="popupWindow">
          <el-input v-model="form.popupWindow" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="推送" prop="push">
          <el-input v-model="form.push" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="webhook" prop="webhook">
          <el-input v-model="form.webhook" placeholder="请输入webhook" />
        </el-form-item>
        <el-form-item label="参数" prop="sendIdParam">
          <el-input v-model="form.sendIdParam" placeholder="请输入参数" />
        </el-form-item>
        <el-form-item label="内容参数" prop="textMsg">
          <el-input v-model="form.textMsg" placeholder="请输入内容参数" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否被删除">
          <el-radio-group v-model="form.deleted">
            <el-radio
              v-for="dict in deletedOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否在使用">
          <el-radio-group v-model="form.isInUse">
            <el-radio
              v-for="dict in isInUseOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="jar文件" prop="jarId">
          <el-input v-model="form.jarId" placeholder="请输入jar文件" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPushconfig, getPushconfig, delPushconfig, addPushconfig, updatePushconfig, exportPushconfig } from "@/api/operate/pushconfig";

export default {
  name: "Pushconfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送通道配置表格数据
      pushconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 推送分类字典
      channelTypeOptions: [],
      // 状态字典
      statusOptions: [],
      // 是否被删除字典
      deletedOptions: [],
      // 是否在使用字典
      isInUseOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelType: null,
        channelServiceCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_push_content_type").then(response => {
      this.channelTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.deletedOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isInUseOptions = response.data;
    });
  },
  methods: {
    /** 查询推送通道配置列表 */
    getList() {
      this.loading = true;
      listPushconfig(this.queryParams).then(response => {
        this.pushconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 推送分类字典翻译
    channelTypeFormat(row, column) {
      return this.selectDictLabel(this.channelTypeOptions, row.channelType);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 是否被删除字典翻译
    deletedFormat(row, column) {
      return this.selectDictLabel(this.deletedOptions, row.deleted);
    },
    // 是否在使用字典翻译
    isInUseFormat(row, column) {
      return this.selectDictLabel(this.isInUseOptions, row.isInUse);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelType: null,
        channelServiceCode: null,
        channelServiceName: null,
        channelName: null,
        popupWindow: null,
        push: null,
        webhook: null,
        sendIdParam: null,
        textMsg: null,
        status: "0",
        deleted: "0",
        isInUse: "0",
        jarId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推送通道配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPushconfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推送通道配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePushconfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPushconfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除推送通道配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPushconfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有推送通道配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPushconfig(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
